package org.hit.burkun.umls.relation;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//过滤出来只出现在hp和do和go中的umls id
//因为clivar和其它各种各样的数据库，数据量实在太多，没办法找到
public class OntologySiddUmlsFilter {
	private static Logger logger = LoggerFactory
			.getLogger(OntologySiddUmlsFilter.class);
	private static Set<String> cacheRes = new HashSet<String>();

	private static Set<String> getFilterUmlsIds() {
		if (cacheRes.size() == 0) {
			initOntologyUmlsId();
			initSiddUmls();
		}
		return cacheRes;
	}

	public static boolean isContain(String item) {
		Set<String> res = getFilterUmlsIds();
		return res.contains(item);
	}

	public static boolean isContain(Collection<String> items) {
		for (String i : items) {
			if (isContain(i)) {
				return true;
			}
		}
		return false;
	}

	private static void initOntologyUmlsId() {
		logger.debug("get unique umls id from hp, do and go...");
		DBHelper dbh = DBHelper.getServerData();
		StringBuilder sb = new StringBuilder();
		sb.append("select umls_cui from map_umls_ids where db_type in ('");
		sb.append(Entity.TYPE.DISEASE_ONTOLOGY.getTag()).append("','");
		sb.append(Entity.TYPE.HP_ONTOLOGY.getTag()).append("','");
		sb.append(Entity.TYPE.GENE_ONTOLOGY.getTag()).append("')");
		dbh.connectionDB();
		Collection<String> res = dbh.queryData(sb.toString());
		dbh.disconnection();
		Set<String> resSet = new HashSet<String>(res);
		cacheRes = resSet;
		cacheRes.addAll(resSet);
	}

	private static void initSiddUmls() {
		logger.debug("get umls id from do mapping table...");
		DBHelper dbh = DBHelper.getLocalBioSearch();
		StringBuilder sb = new StringBuilder();
		sb.append("select otherdb_umls from do_mapping");
		dbh.connectionDB();
		Collection<String> res = dbh.queryData(sb.toString());
		dbh.disconnection();
		Set<String> resSet = new HashSet<String>();
		for (String items : res) {
			String[] item = items.split(",");
			for (String ii : item) {
				resSet.add(ii);
			}
		}
		cacheRes.addAll(resSet);
	}

}
